//
//  SelectLoanTypeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/29.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol SelectLoanTypeViewDelegate {
    @objc optional func selectLoanTitle(title:String) //选择贷款内容
}

class SelectLoanTypeView: UIView {
    
    weak var delegate : SelectLoanTypeViewDelegate?
    
    fileprivate let cellId = "loanCell"
    
    var loanType = 0 //贷款类型
    
    var workType = unlimited //工作类型
    var loanSum = unlimited //贷款额度
    var mortgage = unlimited //抵押类型
    
    var dataArray = [String]() {
        didSet{
            remakeConstraints()
            collectionView.reloadData()
        }
    }
    
    fileprivate var collectionView : UICollectionView!
    
    init(workType:String,loanSum:String,mortgage:String) {
        super.init(frame: .zero)
        
        self.workType = workType
        self.loanSum = loanSum
        self.mortgage = mortgage
        
        if workType == workStatus {
            self.workType = unlimited
        }
        
        if loanSum == loanLimit {
            self.loanSum = unlimited
        }
        
        if mortgage == mortgageType {
            self.mortgage = unlimited
        }
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(SelectLoanTypeCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
    }
    
    //MARK: 设置约束
    fileprivate func remakeConstraints() {
        
        var count = dataArray.count/3
        let count2 = dataArray.count%3
        
        if count2 != 0 {
            count += 1
        }
        
        collectionView.snp.remakeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: CGFloat(count)*RATIO_H(maxNum: 50)))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension SelectLoanTypeView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? SelectLoanTypeCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        
        switch loanType {
        case 1:
            if dataArray[indexPath.row] == workType {
                cell?.titleLabel.backgroundColor = UIColor.red
                cell?.titleLabel.textColor = UIColor.white
            }else{
                cell?.titleLabel.backgroundColor = UIColor.clear
                cell?.titleLabel.textColor = UIColor.darkGray
            }
        case 2:
            if dataArray[indexPath.row] == loanSum {
                cell?.titleLabel.backgroundColor = UIColor.red
                cell?.titleLabel.textColor = UIColor.white
            }else{
                cell?.titleLabel.backgroundColor = UIColor.clear
                cell?.titleLabel.textColor = UIColor.darkGray
            }
        case 3:
            if dataArray[indexPath.row] == mortgage {
                cell?.titleLabel.backgroundColor = UIColor.red
                cell?.titleLabel.textColor = UIColor.white
            }else{
                cell?.titleLabel.backgroundColor = UIColor.clear
                cell?.titleLabel.textColor = UIColor.darkGray
            }
        default:
            break
        }
        
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.delegate?.selectLoanTitle!(title: dataArray[indexPath.row])
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if loanType == 1 && indexPath.row == dataArray.count - 1 {
            return CGSize.init(width: ScreenW/2, height: RATIO_H(maxNum: 50))
        }else {
            return CGSize.init(width: ScreenW/3, height: RATIO_H(maxNum: 50))
        }
        
        
    }
}
